package com.example.touch_demo;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;


import com.example.touch_demo.R;
import com.example.touch_demo.adapter.StudentArrayAdapter;
import com.example.touch_demo.base.BaseActivity;
import com.example.touch_demo.common.WeakRrfHandler;
import com.example.touch_demo.entity.Student;
import com.example.touch_demo.views.RefreshListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListActivity extends BaseActivity implements RefreshListView.OnRefreshingListener {
    private static final int WHAT_REFRESHING = 0;
    private static final int WHAT_LOAD_MORE = 1;

    private RefreshListView mListView;
    private List<Student> studentList;
    Random random = new Random();
    private int min;
    private int max;
    StudentArrayAdapter studentAdapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    public void init() {
        mListView = findViewById(R.id.list);
        initData();
        studentAdapter = new StudentArrayAdapter(this, R.layout.activity_list_item, studentList);
        mListView.setAdapter(studentAdapter);
        mListView.setOnRefreshingListener(this);
    }

    private void initData() {
        min = 0;
        studentList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Student studnet = new Student();
            studnet.setName("王老" + i);
            studnet.setAge(random.nextInt());
            studentList.add(studnet);
            max = i;
        }
    }

    private void refresh() {
        int curnim = min - 1;
        for (int i = curnim; i > curnim - 5; i--) {
            Student studnet = new Student();
            studnet.setName("王老" + i);
            studnet.setAge(random.nextInt());
            studentList.add(0, studnet);
            min = i;
        }
        studentAdapter.notifyDataSetChanged();
    }

    private void loadMore() {
        int currmax = max + 1;
        for (int i = currmax; i < currmax + 5; i++){
            Student studnet = new Student();
            studnet.setName("王老" + i);
            studnet.setAge(random.nextInt());
            studentList.add(studnet);
            max = i;
        }
        studentAdapter.notifyDataSetChanged();
        mListView.notifyDataChange();
    }

    @Override
    public void onRefreshing() {
        Log.d("asdf", "onRefreshing");
        refresh();
        handler.sendEmptyMessageDelayed(WHAT_REFRESHING, 2000);
    }

    @Override
    public void onLoadMore() {
        Log.d("asdf", "onLoadMore");
        loadMore();
        handler.sendEmptyMessageDelayed(WHAT_LOAD_MORE, 2000);
    }

    WeakRrfHandler<ListActivity> handler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_REFRESHING:
                    mListView.onRefreshComplete();
                    break;
                case WHAT_LOAD_MORE:
                    mListView.onLoadmoreComplete();
                    break;
            }

            return false;
        }
    });
}
